package gr.atc.epart;

import gr.atc.epart.actions.Action;
import gr.atc.epart.actions.ActionComment;
import gr.atc.epart.actions.Petition;
import gr.atc.epart.actions.Action.Type;
import gr.atc.epart.net.CreateFailedException;
import gr.atc.epart.net.RetrievalFailedException;

import java.text.SimpleDateFormat;
import java.util.List;

import com.google.gdata.data.extensions.Comments;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Pair;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class ActionDetailsActivity extends Activity {

	private static final int ADD_COMMENT = 0;
	private static final int VIEW_COMMENTS = 1;
	private static final int VIEW_TOPICS = 2;
	private static final int TAKE_SURVEY	= 3;
	private static final int ADD_QUESTIONS	= 4;
	private static final int VIEW_SURVEY_RESULTS	= 5;
	
	boolean signed = false;
	EPartClient client;
	int petitionId;
	// Button signButton;
	TextView signedText;
	TextView actionSignatures;
	int signatures;
	String username;
	Action selectedAction;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.actiondetails);

		username = AccountUtil.getAccountName(this.getBaseContext());
		selectedAction = EPartVolatileDataStore.getInstance()
				.getSelectedAction();

		signedText = (TextView) findViewById(R.id.actiondetails_signed);
		// signButton = (Button) findViewById(R.id.signbutton);
		// signButton.setOnClickListener(signBtnListener);
		actionSignatures = (TextView) findViewById(R.id.actiondetails_signatures);
		actionSignatures.setVisibility(View.INVISIBLE);
		signedText.setVisibility(View.INVISIBLE);
		// signButton.setVisibility(View.INVISIBLE);

		if (selectedAction.getType() == Type.ePetition) {

			actionSignatures.setVisibility(View.VISIBLE);

			petitionId = selectedAction.getId();
			client = new EPartClient();

			Pair<Petition, Boolean> status = null;
			try {
				status = client.getPetitionStatus(petitionId, username);
			} catch (RetrievalFailedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			signed = status.second;
			Petition petition = status.first;
			signatures = petition.getSignaturesCount();
			actionSignatures.setText("Signatures: " + signatures);

			if (signed) {
				signedText.setVisibility(View.VISIBLE);
			} else {
				// signButton.setVisibility(View.VISIBLE);
			}

		}

		if (selectedAction != null) {
			TextView actionName = (TextView) findViewById(R.id.actiondetails_name);
			// TextView actionCreationDate = (TextView)
			// findViewById(R.id.actiondetails_creationdate);
			TextView actionCreator = (TextView) findViewById(R.id.actiondetails_creator);
			TextView actionDescription = (TextView) findViewById(R.id.actiondetails_description);
			TextView actionVisits = (TextView) findViewById(R.id.actiondetails_visit_counter);
			TextView actionPopularity = (TextView) findViewById(R.id.actiondetails_popularity);

			ImageView detailsImage = (ImageView) findViewById(R.id.actiondetails_image);

			Type actionType = selectedAction.getType();

			if (actionType == Type.ePetition) {
				detailsImage.setImageResource(R.drawable.ic_petition);

			} else if (actionType == Type.Deliberation) {
				detailsImage.setImageResource(R.drawable.ic_deliberations);
			} else if (actionType == Type.Survey) {
				detailsImage.setImageResource(R.drawable.ic_survey);
			}

			if (actionName != null)
				actionName.setText(selectedAction.getName());

			String tempCreator = selectedAction.getCreator().getName();
			SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
			String tempDate = formatter.format(selectedAction.getDateCreated());
			String prettyCreationDetails = "By " + tempCreator + " on "
					+ tempDate;
			actionCreator.setText(prettyCreationDetails);

			if (actionDescription != null)
				actionDescription.setText(selectedAction.getDescription());

			if (actionVisits != null)
				actionVisits
						.setText(String.format("%s: %d", getBaseContext()
								.getString(R.string.views), selectedAction
								.getVisits()));

			if (actionPopularity != null)
				actionPopularity.setText(String.format("%s: %d",
						getBaseContext().getString(R.string.popularity),
						selectedAction.getPopularity()));

		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		if (selectedAction.getType() == Type.ePetition && !signed) 
		{
			menu.add(Menu.NONE, ADD_COMMENT, Menu.NONE, "Sign");
		} 
	
		if (selectedAction.getType() == Type.Deliberation)
		{
			menu.add(Menu.NONE, VIEW_TOPICS, Menu.NONE, "View Topics");
		}
		else if (selectedAction.getType() == Type.Survey)
		{
			menu.add(Menu.NONE, ADD_QUESTIONS, Menu.NONE, "Add Questions");
			menu.add(Menu.NONE, TAKE_SURVEY, Menu.NONE  , "Take Survey");
			menu.add(Menu.NONE, VIEW_SURVEY_RESULTS , Menu.NONE, "View Results");
		}
		else
		{
			menu.add(Menu.NONE, VIEW_COMMENTS, Menu.NONE, "View Comments");
		}
		
		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {

		super.onPrepareOptionsMenu(menu);
		if ((signed) && (selectedAction.getType() == Type.ePetition)) {
			menu.removeItem(ADD_COMMENT);
		}

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {

		case ADD_COMMENT:
			Intent displayCommentsIntent = new Intent();
			displayCommentsIntent.setClassName("gr.atc.epart",
					"gr.atc.epart.AddCommentActivity");
			startActivity(displayCommentsIntent);
			return true;

		case VIEW_COMMENTS:
			Intent addCommentsIntent = new Intent();
			addCommentsIntent.setClassName("gr.atc.epart",
					"gr.atc.epart.DisplayCommentsActivity");
			startActivity(addCommentsIntent);
			return true;
				
			
		case VIEW_TOPICS:
			Intent viewDeliberationsTopicsIntent = new Intent();
			viewDeliberationsTopicsIntent.setClassName("gr.atc.epart",
					"gr.atc.epart.DeliberationTopicsActivity");
			startActivity(viewDeliberationsTopicsIntent);
			return true;
			
			
		case TAKE_SURVEY:

			Intent viewSurveyQuestionsIntent = new Intent();
			viewSurveyQuestionsIntent.setClassName("gr.atc.epart",
					"gr.atc.epart.SurveyQuestionsActivity");
			startActivity(viewSurveyQuestionsIntent);
			return true;

		case ADD_QUESTIONS:
			Intent addSurveyQuestionsIntent = new Intent();
			addSurveyQuestionsIntent.setClassName("gr.atc.epart",
					"gr.atc.epart.SurveyQuestionsCreationActivity");
			startActivity(addSurveyQuestionsIntent);

			return true;
			
		case  VIEW_SURVEY_RESULTS:

			Intent viewSurveyResultsIntent = new Intent();
			viewSurveyResultsIntent.setClassName("gr.atc.epart",
					"gr.atc.epart.SurveyResultsActivity");
			startActivity(viewSurveyResultsIntent);

			return true;
			
		default:
			return false;
		}

	}

	public void onRestart() {

		super.onRestart();

		actionSignatures.setVisibility(View.INVISIBLE);
		signedText.setVisibility(View.INVISIBLE);
		// signButton.setVisibility(View.INVISIBLE);

		if (selectedAction.getType() == Type.ePetition) {

			actionSignatures.setVisibility(View.VISIBLE);

			petitionId = selectedAction.getId();
			client = new EPartClient();

			Pair<Petition, Boolean> status = null;
			try {
				status = client.getPetitionStatus(petitionId, username);
			} catch (RetrievalFailedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			signed = status.second;
			Petition petition = status.first;
			signatures = petition.getSignaturesCount();
			actionSignatures.setText("Signatures: " + signatures);

			if (signed) {
				signedText.setVisibility(View.VISIBLE);
			} else {
				// signButton.setVisibility(View.VISIBLE);
			}
		}
	}

	public void createAlert() {

		AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
		dlgAlert.setMessage("You have signed the petition");
		dlgAlert.setTitle("Message");
		dlgAlert.setPositiveButton("OK", null);
		dlgAlert.setCancelable(true);
		signedText.setVisibility(View.VISIBLE);
		// signButton.setVisibility(View.INVISIBLE);
		signatures = signatures + 1;
		actionSignatures.setText("Signatures: " + signatures);
		dlgAlert.create();
		dlgAlert.show();

	}

	
}
